package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//数据返回模块,语法糖函数封装

//直接返回成功
func Success(c *gin.Context, msg string) {
	ReturnJson(c, http.StatusOK, 1, msg, gin.H{})
}

//返回成功-带数据
func SuccessData(c *gin.Context, msg string, data interface{}) {
	ReturnJson(c, http.StatusOK, 1, msg, data)
}

// 直接返回失败
func Fail(c *gin.Context, msg string) {
	ReturnJson(c, http.StatusOK, 0, msg, gin.H{})
	c.Abort()
}


//json返回,默认
func ReturnJson(c *gin.Context, httpCode int, dataCode int, msg string, data interface{}) {
	//Context.Header("key2020","value2020")//可以根据实际情况在头部添加额外的其他信息
	c.Header("Content-Type", "application/json; charset=utf-8")
	c.JSON(httpCode, gin.H{
		"code": dataCode,
		"msg":  msg,
		"data": data,
	})
}

//失败的业务逻辑 - 自定义code和返回参数
func FailData(c *gin.Context, dataCode int, msg string, data interface{}) {
	ReturnJson(c, http.StatusOK, dataCode, msg, data)
	c.Abort()
}


//将json字符串以标准json格式返回（例如，从redis读取json、格式的字符串，返回给浏览器json格式）
func ReturnJsonFromString(c *gin.Context, httpCode int, jsonStr string) {
	c.Header("Content-Type", "application/json; charset=utf-8")
	c.String(httpCode, jsonStr)
}
